package com.experiment04.serviceimpl;

import com.experiment04.entity.Student;
import com.experiment04.service.StudentService;
import static com.experiment04.resource.DatabaseUtils.STUDENTS;

import java.time.Year;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StudentServiceImpl implements StudentService {
    public List<Student> addStudent(Student student){
        STUDENTS.add(student);
        return STUDENTS;
    }
    public List<Student> listStudentsByYear(int year){
        return STUDENTS.stream().filter(student -> student.getYear() == year).collect(Collectors.toList());
    }
    public List<String> listStudentsNames(int year, Student.Sex sex){
        return   STUDENTS
                .stream()
                .filter(student -> student.getYear() == year && student.getSex() == sex)
                .map(Student::getName)
                .collect(Collectors.toList());

    }
    public List<Student> listStudentsByYearSortedById(int year){
       return STUDENTS.stream()
               .filter(student -> student.getYear() == year)
                .sorted(Comparator.comparing(Student::getId).reversed())
                .collect(Collectors.toList());
    }
    public Map<Student.Sex, List<Student>> listStudentsMapBySex(){
     return    STUDENTS.stream()
             .collect(Collectors.groupingBy(Student::getSex));
    }
   public Map<Integer, Student> listStudentsByYearMapById(int year){
        return STUDENTS.stream()
                .filter(student -> student.getYear() == year)
                .collect(Collectors.toMap(Student::getId, student -> student));
    }
    public boolean removeStudent(int id){
        STUDENTS.remove(id);
        List<Student> students = STUDENTS.stream()
                .filter(student -> student.getId() == id)
                .toList();
        return students.isEmpty();
        }
}
